/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TambahGolonganGUI.java
 *
 * Created on 24 Jul 12, 21:45:16
 */
package gui;

import dao.GolonganDAO;
import dao.GolonganDAO;
import entity.Golongan;
import javax.persistence.EntityManager;
import javax.swing.JOptionPane;

/**
 *
 * @author Yust
 */
public class TambahGolonganGUI extends javax.swing.JInternalFrame {

    EntityManager em;
    boolean valid = false;

    /** Creates new form TambahGolonganGUI */
    public TambahGolonganGUI(EntityManager em) {
        initComponents();
        this.em = em;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        kodegolongan_txt = new javax.swing.JTextField();
        namagolongan_txt = new javax.swing.JTextField();
        gajipokok_txt = new javax.swing.JTextField();
        pajak_txt = new javax.swing.JTextField();
        tambah_btn = new javax.swing.JButton();
        reset_btn = new javax.swing.JButton();
        keluar_btn = new javax.swing.JButton();
        cek_btn = new javax.swing.JButton();
        cek_txt = new javax.swing.JLabel();

        setTitle("Tambah Golongan");

        jPanel1.setBackground(new java.awt.Color(204, 255, 255));

        jLabel1.setText("Kode Golongan");

        jLabel2.setText("Nama Golongan");

        jLabel3.setText("Gaji Pokok");

        jLabel4.setText("Pajak (dalam %)");

        tambah_btn.setText("Tambah");
        tambah_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tambah_btnActionPerformed(evt);
            }
        });

        reset_btn.setText("Reset");
        reset_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                reset_btnActionPerformed(evt);
            }
        });

        keluar_btn.setText("Keluar");
        keluar_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                keluar_btnActionPerformed(evt);
            }
        });

        cek_btn.setText("Check");
        cek_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cek_btnActionPerformed(evt);
            }
        });

        cek_txt.setText("-----");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addGap(45, 45, 45)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(tambah_btn)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(reset_btn, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(keluar_btn, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(namagolongan_txt, javax.swing.GroupLayout.DEFAULT_SIZE, 314, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(kodegolongan_txt, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(38, 38, 38)
                        .addComponent(cek_btn, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cek_txt))
                    .addComponent(gajipokok_txt, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pajak_txt, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(kodegolongan_txt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cek_btn)
                    .addComponent(cek_txt))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(namagolongan_txt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(gajipokok_txt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(pajak_txt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tambah_btn)
                    .addComponent(reset_btn)
                    .addComponent(keluar_btn))
                .addContainerGap(22, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void reset_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_reset_btnActionPerformed
        // TODO add your handling code here:
        kodegolongan_txt.setText("");
        namagolongan_txt.setText("");
        gajipokok_txt.setText("");
        pajak_txt.setText("");
        
        cek_txt.setText("-----");
        
        valid = false;
    }//GEN-LAST:event_reset_btnActionPerformed

    private void keluar_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_keluar_btnActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_keluar_btnActionPerformed

    private void tambah_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tambah_btnActionPerformed
        // TODO add your handling code here:
        GolonganDAO dao = new GolonganDAO(em);
        Golongan g = new Golongan();

        String message = "";

        if (kodegolongan_txt.getText().isEmpty() || namagolongan_txt.getText().isEmpty()
                || gajipokok_txt.getText().isEmpty() || pajak_txt.getText().isEmpty()) {
            message = "Harap Isi Data";
            JOptionPane.showMessageDialog(this, message);
        } else if (cek_txt.getText().equalsIgnoreCase("ID Sudah Ada")) {
            message = "ID Sudah Ada";
            JOptionPane.showMessageDialog(this, message);
        } else {

            if (valid == true) {
                try {
                    g.setKodeGolongan(kodegolongan_txt.getText());
                    g.setNamaGolongan(namagolongan_txt.getText());
                    g.setGajiPokok(Double.parseDouble(gajipokok_txt.getText()));
                    g.setPajak(Double.parseDouble(pajak_txt.getText()));
                    JOptionPane.showMessageDialog(this, dao.Insert(g));

                    valid = false;
                    //message = "Sukses Tambah Golongan";
                } catch (NumberFormatException e) {
                    message = "Gaji dan Pajak Harus Diisi dengan Angka";
                    JOptionPane.showMessageDialog(this, message);
                } catch (Exception ex) {
                    ex.printStackTrace();
                    message = ex.getMessage();
                    JOptionPane.showMessageDialog(this, message);
                }

            } else {
                message = "Cek ID Dahulu";
                JOptionPane.showMessageDialog(this, message);
            }



        }
    }//GEN-LAST:event_tambah_btnActionPerformed

    private void cek_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cek_btnActionPerformed
        // TODO add your handling code here:
        if (kodegolongan_txt.getText().isEmpty()) {
            JOptionPane.showMessageDialog(this, "Isi Kode dahulu");
        } else {
            GolonganDAO dao = new GolonganDAO(em);

            if (dao.getAll().size() == 0) {
                cek_txt.setText("ID Tersedia");
                valid = true;
            } else {
                for (int i = 0; i < dao.getAll().size(); i++) {

                    if (kodegolongan_txt.getText().equalsIgnoreCase(dao.getAll().get(i).getKodeGolongan())) {
                        cek_txt.setText("ID Sudah Ada");
                        break;
                    } else {
                        cek_txt.setText("ID Tersedia");
                        valid = true;
                    }

                }
            }



        }
    }//GEN-LAST:event_cek_btnActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cek_btn;
    private javax.swing.JLabel cek_txt;
    private javax.swing.JTextField gajipokok_txt;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton keluar_btn;
    private javax.swing.JTextField kodegolongan_txt;
    private javax.swing.JTextField namagolongan_txt;
    private javax.swing.JTextField pajak_txt;
    private javax.swing.JButton reset_btn;
    private javax.swing.JButton tambah_btn;
    // End of variables declaration//GEN-END:variables
}
